package com.sasu.university_data.controller.after;

import com.sasu.university_data.entity.WorkPlace;
import com.sasu.university_data.service.WorkPlaceService;
import com.sasu.university_data.util.ResponseCode;
import com.sasu.university_data.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "毕业去向功能接口")
@RestController
@RequestMapping("/work-place")
public class WorkPlaceController {

    @Resource
    private WorkPlaceService workPlaceService;

    /***
     * 分页查询数据
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询数据")
    @GetMapping("/select")
    public ResponseData selectByPage( String loc, Integer page, Integer pageSize){

        List<WorkPlace> workPlaces = workPlaceService.selectByPage(loc, page, pageSize);

        if (workPlaces==null){
            return new ResponseData(ResponseCode.FAIL);
        }

        long count = workPlaceService.count(loc);

        return new ResponseData(ResponseCode.SUCCESS,workPlaces,count);
    }
    /**
     * 新增数据
     *
     * @param workPlace 实例对象
     * @return 是否成功
     */
    @ApiOperation("新增数据")
    @PostMapping("/add")
    public ResponseData add(@RequestBody WorkPlace workPlace){

        if (!workPlaceService.insert(workPlace)){
            return new ResponseData(ResponseCode.ADD_FAILED);
        }

        long count = workPlaceService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }
    /**
     * 更新数据
     *
     * @param workPlace 实例对象
     * @return 是否成功
     */
    @ApiOperation("更新数据")
    @PostMapping("/update")
    public ResponseData edit(@RequestBody WorkPlace workPlace){

        if (!workPlaceService.update(workPlace)){
            return new ResponseData(ResponseCode.UPDATE_FAILED);
        }

        long count = workPlaceService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /**
     * 通过主键删除数据
     *
     * @param mId 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @PostMapping("/delete")
    public ResponseData deleteById(Integer mId){

        if (!workPlaceService.deleteById(mId)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = workPlaceService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /***
     * 通过id批量删除数据
     * @param wIds
     * @return 是否成功
     */
    @ApiOperation("通过id批量删除数据")
    @PostMapping("/lotDelete")
    public ResponseData deleteByIds( int[] wIds){

        if (!workPlaceService.deleteByIds(wIds)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = workPlaceService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }
}
